﻿using System;

using DreamCube.WinForm.Common;
using DreamCube.WebServer.WinServiceModel;
using DreamCube.Foundation.Basic.Utility;

namespace WebServer.WinServiceManager
{
    /// <summary>
    /// windows app的服务类型
    /// </summary>
    class WinAppService : Service
    {
        public override MyWinService.ServiceStatus Status
        {
            get
            {
                if (MyWinService.GetServiceStatusEx(Common.KeepLiveServiceName) != MyWinService.ServiceStatus.NotExist)
                {
                    if (DreamCube.Foundation.Basic.Utility.MyProcess.IsExistProcess(Common.IISProcessName))
                        return MyWinService.ServiceStatus.Running;
                    else
                        return MyWinService.ServiceStatus.Stopped;
                }
                else
                {
                    return MyWinService.ServiceStatus.NotExist;
                }
            }
        }

        public override void Stop()
        {
            //MyProcess.KillProcessByName(Common.IISProcessName, 1000 * 60);
            MyWinService.StopService(Common.KeepLiveServiceName, new TimeSpan(0, 0, 20));
        }

        public override void Start()
        {
            if (!String.IsNullOrEmpty(Common.IISAppPath))
            {
                MyCMD.RunEXE(Common.IISAppPath, "true", null, false);
                //30秒为超时时间
                MyWinService.StartService(Common.KeepLiveServiceName, new TimeSpan(30 * 1000));
            }
        }
    }
}
